<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
		.mini-checkboxlist-item{
			padding: 5px;
		}
	</style>
</head>

<body>
	<div id="form1">
        <input labelField="true" label="企业名称：" id="textbox1" vtype="maxLength:100" name="name" class="mini-textbox" required="true" />
        <input name="id" class="mini-hidden" required="true"/>
        <input id="old_name" class="mini-hidden" />
        <input labelField="true" label="企业描述：" style="width:400px;" id="textarea1" name="description" vtype="maxLength:500" class="mini-textarea" required="true"/>
        <input id="provinceCombo" labelField="true" label="所在省：" class="mini-combobox" name="provinceId" textField="pName" valueField="id" 
	        onvaluechanged="onChanged" url="medical_find_province.do" required="true" />
	    <input id="cityCombo" labelField="true" label="所在市：" class="mini-combobox" name="cityId" required="true" textField="cName" valueField="id" />
	    
		<!--外线号码 -->
	    <div id="outside" class="mini-combobox" labelField="true" label="外线号码：" popupWidth="150" textField="outSideNum" valueField="id" 
			multiSelect="true" >
		</div>
		
		<!--队列 -->
		<div id="cb_queue" labelField="true" label="队列号：" class="mini-checkboxlist" repeatItems="10" repeatLayout="table" textField="queueNo" valueField="id" value=""></div>
		
		<!--分机 -->
		<div id="cb_extension" labelField="true" label="分机号：" class="mini-checkboxlist" repeatItems="10" repeatLayout="table" textField="extension" valueField="id" value=""></div>
		
		<input class="mini-hidden" id="eid" value="<sec:authentication property='principal.enterpriseId'/>" />
       	<div labelField="true" label=" 财务操作：" name="needStatus" class="mini-checkboxlist" repeatItems="2" repeatLayout="table" data=[{id:"1",text:"对账"},{id:"2",text:"回款"}]></div>
        <div style="text-align:left;padding:20px;">
	        <input value="保存" type="button" onclick="onOk()" />
	        <input value="取消" type="button" onclick="onCancel()" />
	    </div>
	</div>
    <div class="description">
        <h3>说明：</h3>
        <p>财务操作为系统是否需要财务人员进行订单对账和订单回款,需要则勾选。</p>
    </div>
</body>
<script type="text/javascript">
	mini.parse();
	var form = new mini.Form("#form1");
	
	var provinceCombo = mini.get("provinceCombo");
    var cityCombo = mini.get("cityCombo");
    var outside = mini.get("outside");
    var cbQueue = mini.get("cb_queue");
    var cbExtension = mini.get("cb_extension");
    var eid = mini.get("eid").getValue();
    
	$(function() {
		
		// 设置外线号码
	    $.post("enterprise_outside_view.do", function(data) {
	    	var usedOutside = new Array();
	    	outside.setData(data);
	    	for(var i=0; i<data.length; i++) {
	    		if(data[i].eid == eid) {
	    			usedOutside.push(data[i].id);
	    		}
	    	}
	    	outside.setValue(usedOutside.join(","));
	    }, "json");
	    
	    // 设置队列号
	    $.post("enterprise_queue_view.do", function(data) {
	    	var usedQueue = new Array();
	    	cbQueue.setData(data);
	    	for(var i=0; i<data.length; i++) {
	    		if(data[i].eid == eid) {
	    			usedQueue.push(data[i].id);
	    		}
	    	}
	    	cbQueue.setValue(usedQueue.join(","));
	    }, "json");
	    
	    // 设置分机号
	    $.post("enterprise_extension_view.do", function(data) {
	    	var usedExtension = new Array();
	    	cbExtension.setData(data);
	    	for(var i=0; i<data.length; i++) {
	    		if(data[i].eid == eid) {
	    			usedExtension.push(data[i].id);
	    		}
	    	}
	    	cbExtension.setValue(usedExtension.join(","));
	    }, "json");
		
		$.post("enterprise_view.do", null, function(info){
	    	 mini.get("old_name").setValue(info.data.name);
	    	 form.setData(info.data);
	    	 onChanged();
	    	 cityCombo.setValue(info.data.cityId);
	    	 if("3" == info.data.needStatus) {
	    		 mini.getbyName("needStatus").setValue("1,2");
	    	 }
	    }, "json");
		
	});

	// 绑定外线号码
	function bindOutside(outside) {
		$.post("enterprise_outside_bind.do", {outSideNums: outside});
	}
	
	// 绑定队列号
	function bindQueue(queue) {
		$.post("enterprise_queue_bind.do", {queue: queue});
	}

	// 绑定分机
	function bindExtension(extension) {
		$.post("enterprise_extension_bind.do", {extension: extension});
	}
	
	function onOk() {
        var o = form.getData(true);
        form.validate();
        if (form.isValid() == false) return;
        var oldName = mini.get("old_name").getValue();
        var name = mini.getbyName("name").getValue();
        if(name != oldName) {
        	$.post("medical_enterprise_include.do", {name: name}, function(info){
        		if(info.success) {
        			save(o);
        			bindOutside(outside.getValue());
        			bindQueue(cbQueue.getValue());
        			bindExtension(cbExtension.getValue());
        		} else {
        			mini.alert('企业名称已存在');
        		}
        	}, "json");
        } else {
        	save(o);
        	bindOutside(outside.getValue());
			bindQueue(cbQueue.getValue());
        	bindExtension(cbExtension.getValue());
        }
    }
	
	function save(o) {
		$.ajax({
	        url: "medical_enterprise_save.do",
			type: 'post',
			dataType: 'json',
	        data: o,
	        cache: false,
	        success: function (text) {
	            if(text.success){
	            	mini.showMessageBox({
	                    title: "提示",
	                    showModal:false,
	                    iconCls: "mini-messagebox-info",
	                    message: "保存成功！",
	                    timeout: 3000,
	                    x: 'right',
	                    y: 'bottom'
	                });
	            }else{
	            	mini.alert("数据错误，请重新添加");
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
	}

    function onChanged() {
        var id = provinceCombo.getValue();
        cityCombo.setValue("");
        var url = "medical_find_citys.do?provinceId=" + id;
        cityCombo.setUrl(url);
        cityCombo.select(0);
    }
    
    function onCancel(e) {
        window.CloseOwnerWindow("cancel");
    }
</script>